#include "Pinger.h"
SK

Pinger::Pinger ()
{
    
}

Pinger::~Pinger ()
{
    
}


void Pinger::ping (const String& src, const String& dest)
{
    RawSocket *soc = new RawSocket (IPPROTO_ICMP);

    IPPacket p;
    p.lenAndVer = 0x45;
    p.tos = 0;
    p.length = sizeof (IPPacket) + sizeof (ICMPPacket);
    p.identification = htons (rand());
    p.ttl = 255;
    p.protocol = IPPROTO_ICMP;

    char lHost [200];
    gethostname(lHost, 200);
    addrinfo *inf;
    p.ucSource = inet_addr (src.c_str());
    p.ucDestination = inet_addr (dest.c_str());
    p.checksum = 0;
    p.checksum = RawSocket::calculateIPChecksum ((byte *)&p, sizeof (IPPacket));

    ICMPPacket ic;

    ic.msgType = 0;
    ic.code = 0;
    ic.id = 0;
    ic.seq = 0;
    ic.checksum = 0;
    ic.checksum = RawSocket::calculateIPChecksum ((byte *)&ic, sizeof (ICMPPacket));

    u32 pSize = sizeof (IPPacket) + sizeof (ICMPPacket);
    byte *packet = new byte [pSize];
    memcpy (packet, &p, sizeof (IPPacket));
    memcpy (&packet [sizeof (IPPacket)], &ic, sizeof (ICMPPacket));

    soc->sendTo (packet, pSize, dest);

    byte *buf = new byte [pSize];

    int ret = soc->recvFrom (buf, pSize, dest);

    delete soc;
}


EK